package xian.woniuxy.zj_list_to_map;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 如何把一个List转换为Map

class User {
    private int id;
    private String name;
    private String addr;

    public User() {
    }

    public User(int id, String name, String addr) {
        this.id = id;
        this.name = name;
        this.addr = addr;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", addr='" + addr + '\'' +
                '}';
    }
}

public class App {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        list.add(new User(1, "andy", "XA"));
        list.add(new User(2, "eaons", "BJ"));
        list.add(new User(3, "haha", "TC"));
        list.add(new User(4, "hehe", "XY"));
        list.add(new User(5, "xixi", "CD"));

//        List<Integer> ids = list.stream().map(u -> u.getId()).collect(Collectors.toList());

        Map<Integer, User> map = list.stream().collect(Collectors.toMap(User::getId, u -> u));

        map.forEach((k,v) -> {
            System.out.println(k + ": " + v);
        });

    }
}
